<template>
  <div class="chooser-component">
    <ul class="chooser-list">
      <li
        v-for="(item, index) in selections"
        :key="item.num"
        @click="toggleSelection(index)"
        :title="item.label"
        :class="{ active: checkActive(index) }"
      >
        {{ item.label }}
      </li>
    </ul>
  </div>
</template>

<script>
import _ from "lodash";
export default {
  name: "multiplyChooser",
  props: {
    selections: {
      type: Array,
      default: () => [
        {
          label: "test",
          value: 0,
        },
      ],
    },
  },
  data() {
    return {
      selectionss: [],
      nowIndexes: [0], //默认选中
    };
  },
  methods: {
    toggleSelection(index) {
      // console.log(this.nowIndexes);
      if (this.nowIndexes.indexOf(index) === -1) {
        this.nowIndexes.push(index);
      } else {
        this.nowIndexes = _.remove(this.nowIndexes, (idx) => {
          return idx !== index;
        });
      }
      let nowObjArray = _.map(this.nowIndexes, (idx) => {
        return this.selectionss[idx];
      });  
      this.$emit("on-change", nowObjArray);
    },
    checkActive(index) {
      // console.log(index);
      return this.nowIndexes.indexOf(index) !== -1;
    },
  },
};
</script>

<style scoped>
.chooser-component {
  position: relative;
  display: inline-block;
}
.chooser-list li {
  display: inline-block;
  border: 1px solid #e3e3e3;
  height: 25px;
  line-height: 25px;
  padding: 0 8px;
  margin-right: 5px;
  border-radius: 3px;
  text-align: center;
  cursor: pointer;
}
.chooser-list li.active {
  border-color: #4fc08d;
  background: #4fc08d;
  color: #fff;
}
</style>
